package com.practice.ajCollections010.map.treemap;

import com.practice.afIO006.In;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 遍历
 */
public class Traversal {
    public static void main(String[] args) {
        TreeMap<Integer, Integer> treeMap = new TreeMap<>();
        for (int i = 0; i < 10; i++) {
            treeMap.put(i, i * i);
        }

        // 遍历方式一：
        for (Integer item : treeMap.keySet()) {
            // do something
            System.out.println(treeMap.get(item));
        }

        // 遍历方式二：
        System.out.println("==========");
        for (Map.Entry<Integer, Integer> entry : treeMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        System.out.println("==========");
        for (Integer v : treeMap.values()) {
            System.out.println(v);
        }

        //
        System.out.println("==========");
        Iterator<Map.Entry<Integer, Integer>> iterator = treeMap.entrySet().iterator();
        while (iterator.hasNext()) {
            // do something
            Map.Entry<Integer, Integer> next = iterator.next();
            System.out.println(next.getKey() + " : " + next.getValue());
        }

        System.out.println("==========");
        treeMap.forEach((K, V) -> {
            System.out.println(K + " : " + V);
        });

        System.out.println("==========");
        treeMap.keySet().forEach(item -> {
            System.out.println(treeMap.get(item));
        });

        System.out.println("==========");
        treeMap.keySet().stream().forEach(item -> {
            System.out.println(treeMap.get(item));
        });

        System.out.println("==========");
        treeMap.entrySet().forEach(item -> {
            System.out.println(item.getKey() + " : " + item.getValue());
        });

        System.out.println("==========");
        treeMap.entrySet().stream().forEach(item -> {
            System.out.println(item.getKey() + " : " + item.getValue());
        });

    }
}
